<template>
    <div class="common-layout">

        <el-container class="content">

            <!-- 侧边栏容器 -->
            <el-aside :width="settingStore.asideWidth" class="aside-container">
                <el-scrollbar>
                    <Menu></Menu>
                </el-scrollbar>
            </el-aside>

            <!-- 主体容器 -->
            <el-container>
                <!-- 头部容器 -->
                <el-header class="header-container">
                    <el-row :gutter="20" class="row-breadcrumb">
                        <!-- 面包屑 左边-->
                        <el-col :span="8">
                            <el-row>
                                <!-- 切换图标 -->
                                <el-col :span="2">
                                    <SvgIcon class="mouse_style" :name="settingStore.foldIcon" color="#f50"
                                        @click="settingStore.isFold = !settingStore.isFold">
                                    </SvgIcon>
                                </el-col>
                                <!-- 面包屑组件 -->
                                <el-col :span="22">
                                    <el-breadcrumb separator="/" @click="handleBreadcrumb">
                                        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
                                        <el-breadcrumb-item v-if="settingStore.menuActive.name !== '/index'">{{
                                            settingStore.menuActive.label }}</el-breadcrumb-item>
                                    </el-breadcrumb>
                                </el-col>
                            </el-row>
                        </el-col>
                        <!-- 右边 个人信息和设置 -->
                        <el-col :span="6" :offset="10">
                            <el-row :gutter="20">
                                <el-col class="right-Svg" :span="3">
                                    <SvgIcon name="搜索" width="18px" height="18px"></SvgIcon>
                                </el-col>
                                <el-col class="right-Svg" :span="3">
                                    <SvgIcon name="源码地址" width="18px" height="18px"></SvgIcon>
                                </el-col>
                                <el-col class="right-Svg" :span="3">
                                    <SvgIcon name="文档地址" width="18px" height="18px"></SvgIcon>
                                </el-col>
                                <el-col class="right-Svg" :span="3">
                                    <SvgIcon name="全屏" width="18px" height="18px"></SvgIcon>
                                </el-col>
                                <el-col class="right-Svg" :span="3">
                                    <SvgIcon name="布局大小" width="18px" height="18px"></SvgIcon>
                                </el-col>
                                <el-col class="right-Svg" :span="4">
                                    <el-dropdown @command="handleCommand" trigger="click">
                                        <el-avatar shape="square" :src="imgUrl"></el-avatar>
                                        <template #dropdown>
                                            <el-dropdown-menu>
                                                <el-dropdown-item command="personal-center">个人中心</el-dropdown-item>
                                                <el-dropdown-item command="layout-settings">布局设置</el-dropdown-item>
                                                <el-dropdown-item command="quit">退出登录</el-dropdown-item>
                                            </el-dropdown-menu>
                                        </template>
                                    </el-dropdown>
                                </el-col>
                            </el-row>
                            <!-- <div>占据内容区</div> -->
                        </el-col>
                    </el-row>
                    <!-- 选项卡 -->
                    <el-row>
                        <el-col :span="24">
                            <el-dropdown trigger="contextmenu" @command="tabHandleCommand">
                                <el-tabs closable v-model="settingStore.menuActive.name" type="card"
                                    @tab-click="handleTabClick" @tab-remove="handleTabRemove">
                                    <el-tab-pane v-if="settingStore.tabs.length > 0" v-for="item in settingStore.tabs"
                                        :key="item.name" :label="item.label" :name="item.name">
                                    </el-tab-pane>
                                </el-tabs>
                                <template #dropdown>
                                        <el-dropdown-item :icon="RefreshLeft" command="refresh">刷新页面</el-dropdown-item>
                                        <el-dropdown-item :icon="CircleClose" command="closeAll">全部关闭</el-dropdown-item>
                                    </template>
                            </el-dropdown>
                        </el-col>
                    </el-row>
                </el-header>
                <!-- 视图容器 -->
                <el-main class="main-container">
                    <RouterView v-slot="{ Component }">
                        <Transition name="bounce" mode="out-in">
                            <component :is="Component" />
                        </Transition>
                    </RouterView>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script setup >
import SvgIcon from '@/components/svgIcon/SvgIcon.vue';
import Menu from '@/components/menu/Menu.vue';
import { onMounted, ref } from 'vue';
import { useHomeStore } from '@/stores/index'
import { useLayoutStore } from '@/stores/settings'
import { getUserInfo } from '@/utils/index'
import router from '@/router/index';
import { logout } from '@/apis/index'
import { ElMessage } from 'element-plus';
import {CircleClose, RefreshLeft } from '@element-plus/icons-vue'
const imgUrl = "src\\assets\\imgs\\1665907878452.png"
const store = useHomeStore()
const settingStore = useLayoutStore()

/**
 * 添加标签页
 * @param tab 标签页
 */
const handleTabClick = (tab) => {
    settingStore.menuActive.name = tab.props.name
    settingStore.menuActive.label = tab.props.label
    router.push({ path: tab.props.name, replace: true })
}
/**
 * 移除标签页
 * @param tab 标签页
 */
const handleTabRemove = (tab) => {
    if (tab == '/index') {
        return;
    }
    // 移除标签页
    settingStore.tabs = settingStore.tabs.filter(item => item.name !== tab);
    // 如果移除的是当前激活的标签页，选择一个新的激活项
    if (settingStore.menuActive.name === tab) {
        // 检查是否有其他标签页
        if (settingStore.tabs.length > 0) {
            // 设置最后一个标签页为激活状态
            settingStore.menuActive.name = settingStore.tabs[settingStore.tabs.length - 1].name;
            settingStore.menuActive.label = settingStore.tabs[settingStore.tabs.length - 1].label;
        } else {
            // 如果没有其他标签页，可以重定向到首页或其他默认页面 // 或者其他默认路径
            settingStore.menuActive.name = '/index'
            settingStore.menuActive.label = '首页'
        }
    }
    // 更新路由
    router.push({ path: settingStore.menuActive.name, replace: true })
}
/**
 * 点击面包屑，返回首页
 * @param e 点击事件
 */
const handleBreadcrumb = (e) => {
    settingStore.menuActive.name = '/index'
    settingStore.menuActive.label = '首页'
}
// 处理右侧个人中心下拉菜单点击事件
const handleCommand = async (command) => {
    if (command == 'personal-center') {
        console.log('个人中心')
    } else if (command == 'layout-settings') {
        console.log('布局设置')
        if (settingStore.tabs.length <= 0 || !settingStore.tabs.some(item => item.name == '/manage/systemManagement/logManagement/lLogin')) {
            settingStore.tabs.push({
                name: '/manage/systemManagement/logManagement/lLogin',
                label: '分配用户',
            })
        }
        settingStore.menuActive.name = '/manage/systemManagement/logManagement/lLogin'
        router.push('/manage/systemManagement/logManagement/lLogin')
    } else if (command == 'quit') {
        await logout()
        localStorage.clear()
        router.push("/")
        ElMessage.success("注销成功")
    }
}
// 处理选项卡右键菜单点击事件
const tabHandleCommand = (command) => {
    if (command == 'refresh') {
         router.push({ path: settingStore.menuActive.name, replace: true })
    } else if (command == 'closeAll') {
        settingStore.tabs = [{name:'/index',label:'首页'}]
        settingStore.menuActive.name = '/index'
        settingStore.menuActive.label = '首页'
        router.push({ path: settingStore.menuActive.name, replace: true })
    }
}

</script>

<style scoped>
.grid-content {
    border-radius: 4px;
    min-height: 36px;
}

.el-menu {
    border: none;
}

.content {
    height: 100vh;
}

.aside-container {
    background: #d3dce6;
    background-color: #304156;
    transition: width 0.3s;
}

.mouse_style {
    cursor: pointer;
}

.header-container {
    height: 80px;
}

.row-breadcrumb {
    height: 20px;
    margin-top: 15px;
}

.right-Svg {
    text-align: center;
    line-height: 44px;
    cursor: pointer;
}

.main-container {
    background: #f0f2f5;
}

/* 路由切换动画 */
.bounce-enter-active {
    /* animation: bounce-in 0.5s; */
    /* animation: bounce-in 1s .2s ease both */
}

.bounce-leave-active {
    /* animation: bounce-in 0.5s reverse; */
    animation: bounce-in 1s .2s ease reverse
}

@-webkit-keyframes bounce-in {
    0% {
        opacity: 0;
        -webkit-transform: translateX(2000px)
    }

    60% {
        opacity: 1;
        -webkit-transform: translateX(-30px)
    }

    80% {
        -webkit-transform: translateX(10px)
    }

    100% {
        -webkit-transform: translateX(0)
    }
}
</style>
